package com.xhwl.centiotdata.handler.pedestrian.impl;

import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationFactory;
import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.centiotdata.service.cent.IAccountCentService;
import com.xhwl.centiotdata.service.cent.IAuthAccountCentService;
import com.xhwl.centiotdata.service.cent.IPedestrianCentService;
import com.xhwl.centiotdata.service.impl.DeviceServiceImpl;
import com.xhwl.centiotdata.suppliersystem.bosiny.PedestrianBosinyHandler;
import com.xhwl.common.dto.auth.PedestrianAuthDTO;
import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.dto.cent.pedes.OpenDoorRecordDTO;
import com.xhwl.common.dto.cent.pedes.UserDTO;
import com.xhwl.common.dto.cent.sdata.AccountDTO;
import com.xhwl.common.enums.CategoryEnum;
import com.xhwl.common.enums.ProductEnum;
import com.xhwl.common.enums.pedestrian.DoorActionEnum;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.query.cent.sdata.AccountQuery;
import com.xhwl.common.utils.ChkUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Falcon.cao @Copyright (C), 2022, 兴海物联
 * Date: 2022/6/10 14:27
 * Description: 人行
 */
@Service
public class PedestrianIntegrationBosinyHandler extends PedestrianIntegrationHandler {

    @Autowired
    private PedestrianBosinyHandler pedestrianBosinyHandler;

    @Autowired
    private DeviceServiceImpl deviceService;
    @Autowired
    private IPedestrianCentService pedestrianCentService;
    @Autowired
    private IAccountCentService accountCentService;

    @Autowired
    private IAuthAccountCentService authAccountCentService;

    @Override
    public void afterPropertiesSet() throws Exception {
        PedestrianIntegrationFactory.register(ProductEnum.BOSINY.getId(), this);
    }

    @Override
    public ResultJson openDoor(PedestrianIntegrationConfigDTO dto, DeviceDTO device, DoorActionEnum action) {
        if (DoorActionEnum.REMOTELY_OPEN != action) {
            return ResultJson.fail("无对应设备操作");
        }
        String message = pedestrianBosinyHandler.openDoor(dto, device);
        return "SUCCESS".equals(message) ? ResultJson.success() : ResultJson.fail(message);
    }

    @Override
    public void pullRecordStatistics(PedestrianIntegrationConfigDTO configDTO, Integer intervalDays) {
        // 拉取数据
        List<OpenDoorRecordDTO> recordBosinyDtoList = pedestrianBosinyHandler.pullRecord(configDTO);
        if (ChkUtil.isNotNull(recordBosinyDtoList)) {
            pedestrianCentService.saveBatch(recordBosinyDtoList);
        }
    }

    @Override
    public ResultJson createAuth(PedestrianIntegrationConfigDTO configDTO, PedestrianAuthDTO pedestrianAuthDTO) {
        List<DeviceDTO> deviceDTOS = deviceService.findDevice(pedestrianAuthDTO.getIndustryId(), null, CategoryEnum.DOOR.getId(), pedestrianAuthDTO.getDeviceIdList());
        if (pedestrianAuthDTO.getUserDTO() != null) {
            return ResultJson.success(pedestrianBosinyHandler.pushCard(configDTO, deviceDTOS, pedestrianAuthDTO));
        }
        deviceDTOS = deviceDTOS.stream().filter(item -> ProductEnum.BOSINY.getId().equals(item.getProductId())).collect(Collectors.toList());
        if (ChkUtil.isNull(deviceDTOS)) {
            return ResultJson.success("Success");
        }
        List<Integer> accountIds = Optional.ofNullable(pedestrianAuthDTO.getAccountIds()).orElse(new LinkedList<>());
        Optional.ofNullable(pedestrianAuthDTO.getAccountId()).ifPresent(accountIds::add);

        if (ChkUtil.isNull(accountIds)) {
            return ResultJson.success("Success");
        }

        AccountQuery query = new AccountQuery();
        query.setIdList(accountIds);
        List<AccountDTO> accounts = accountCentService.getList(query);
        if (ChkUtil.isNull(accounts)) {
            UserDTO userDTO = new UserDTO();
            userDTO.setName("未知");
            return ResultJson.success(pedestrianBosinyHandler.pushCard(configDTO, deviceDTOS, pedestrianAuthDTO));
        }
        for (AccountDTO account : accounts) {
            UserDTO user = new UserDTO();
            user.setName(account.getName());
            user.setPhone(account.getPhone());
            pedestrianAuthDTO.setUserDTO(user);
            pedestrianBosinyHandler.pushCard(configDTO, deviceDTOS, pedestrianAuthDTO);
        }

        try {
            onSavingAuthCards(authAccountCentService, configDTO, pedestrianAuthDTO);
        } catch (Exception e) {
            // 保存转换卡号数据失败
        }
        return ResultJson.success("Success");
    }

    @Override
    public ResultJson updateAuth(PedestrianIntegrationConfigDTO configDTO, PedestrianAuthDTO pedestrianAuthDTO) {
        return ResultJson.fail("未知错误");
    }

    @Override
    public ResultJson deleteAuth(PedestrianIntegrationConfigDTO configDTO, PedestrianAuthDTO pedestrianAuthDTO) {
        List<DeviceDTO> deviceDTOS = deviceService.findDevice(pedestrianAuthDTO.getIndustryId(), null, CategoryEnum.DOOR.getId(), pedestrianAuthDTO.getDeviceIdList());
        pedestrianBosinyHandler.delsCard(configDTO, deviceDTOS, pedestrianAuthDTO);
        return ResultJson.success();
    }

}
